import axios from "axios";
import router from '@/router/index.js'
import {getToken, removeUserInfo} from "@/util/LocalUtils.js";

const request = axios.create({
  baseURL: "http://localhost:8081",
  timeout: 5000,
});

request.interceptors.request.use(config => {
  const token = getToken()
  config.headers.setAuthorization(token)
  return config
})
request.interceptors.response.use(res => {
  const data = res.data
  if (data.code === 444) {
    removeUserInfo()
    router.push("/login")
  }
  return Promise.resolve(data)
}, error => {
  return Promise.reject(error)
})

export const reqLogin = (data) => request({url: "/user/login", method: "post", data});
export const reqUserInfo = () => request({url: "/user/info", method: "get"});
export const reqFriendList = () => request({url: "/friend/list", method: "get"});
export const reqOfflineMessageList = (params) => request({url: "/message/offline/list", method: "get", params});
export const reqImServiceInfo = () => request({url: "/message/active/service/info", method: "get"});

export const reqFilePolicy = (params) => request({url: "/file/policy", method: "get", params});

export const fileUpload = (data) => {
  return axios.post("http://121.41.39.119:8100/test001", data)
}